package com.sunday.volunteer.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Activity;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.utils.ViewPagerUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/1/10.
 */

public class IndexAdapter extends RecyclerViewAdapter {
    private SparseArray sparseArray;


    public IndexAdapter(Object o, int[] layout) {
        super(o, layout);
        sparseArray = (SparseArray) o;
    }


    @Override
    public int getItemCount() {
        if (sparseArray != null && sparseArray.get(1) != null) {
            return 1 + ((List<Activity>) sparseArray.get(1)).size();
        }
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position != 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(View itemView, int viewType) {
        switch (viewType) {
            case 0:
                return new ViewHolderHeader(itemView);
            default:
                return new ViewHolderBody(itemView);
        }

    }

    public class ViewHolderHeader extends MyViewHolder {
        @Bind(R.id.viewpager)
        ConvenientBanner viewPager;
        @Bind(R.id.hours)
        TextView hours;
        @Bind(R.id.ranking)
        TextView ranking;
        @Bind(R.id.to_raking)
        View to_raking;
        @Bind(R.id.tab01)
        RadioButton tab01;
        @Bind(R.id.tab02)
        RadioButton tab02;
        @Bind(R.id.tab03)
        RadioButton tab03;
        public ViewHolderHeader(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            to_raking.setOnClickListener(onClickListener);
            tab01.setOnClickListener(onClickListener);
            tab02.setOnClickListener(onClickListener);
            tab03.setOnClickListener(onClickListener);
            setHours(hours);
            setRanking(ranking);
            setTab01(tab01);
            setTab02(tab02);
            setTab03(tab03);
        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {
            SparseArray sparseArray = (SparseArray) object;
            if (sparseArray.keyAt(0) != -1 && sparseArray.get(0) != null) {
                List<Images> list = (List<Images>) sparseArray.get(0);
                if (list != null) {
                    new ViewPagerUtils(viewPager.getContext(), viewPager, list, DeviceUtils.getDisplay(viewPager.getContext()).widthPixels / 2 ,false);
                }
            }

        }
    }

    public class ViewHolderBody extends MyViewHolder {
        @Bind(R.id.image_view)
        ImageView imageView;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.count)
        TextView count;
        @Bind(R.id.group_name)
        TextView group_name;
        @Bind(R.id.hours)
        TextView hours;
        @Bind(R.id.btn)
        TextView btn;

        public ViewHolderBody(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(onClickListener);
        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {
            SparseArray sparseArray = (SparseArray) object;
            if (sparseArray.keyAt(0) != -1 && sparseArray.get(1) != null) {
                List<Activity> list = (List<Activity>) sparseArray.get(1);
                if (list != null && list.get(position - 1) != null) {

                    Activity item = list.get(position - 1);
                    setImage(imageView, item.getMainImage(), 125, 85);
                    setText(name, item.getName());
                    setText(time, item.getTime());
                    setText(address, item.getAddress());
                    setText(count, item.getJoinNumber() + "人");
                    setText(group_name, String.format("来自 %s", item.getGroupName()));
                    setText(hours, String.format("时长 %.0f%s", item.getHours(), "h"));

                    btn.setVisibility(item.getStatus() == 3 ? View.GONE : View.VISIBLE);
                    hours.setVisibility(item.getStatus() == 3 ? View.VISIBLE : View.GONE);

                    itemView.setTag(item.getId());
                }
            }
        }
    }


    private TextView hours;
    private TextView ranking;
    private RadioButton tab01;
    private RadioButton tab02;
    private RadioButton tab03;

    public TextView getHours() {
        return hours;
    }

    public void setHours(TextView hours) {
        this.hours = hours;
    }


    public TextView getRanking() {
        return ranking;
    }

    public void setRanking(TextView ranking) {
        this.ranking = ranking;
    }

    public RadioButton getTab01() {
        return tab01;
    }

    public void setTab01(RadioButton tab01) {
        this.tab01 = tab01;
    }

    public RadioButton getTab02() {
        return tab02;
    }

    public void setTab02(RadioButton tab02) {
        this.tab02 = tab02;
    }

    public RadioButton getTab03() {
        return tab03;
    }

    public void setTab03(RadioButton tab03) {
        this.tab03 = tab03;
    }
}
